package com.smart_campus.teg.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.domain.User;
import com.smart_campus.teg.domain.Major;
import com.smart_campus.teg.vo.CourseVo;
import com.smart_campus.teg.vo.MajorVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.smart_campus.teg.mapper.CourseMapper;
import com.smart_campus.teg.domain.Course;
import com.smart_campus.teg.service.CourseService;

import java.util.List;
import java.util.Map;

/**
*   @aytgor 陈鹏
*   @date 2020/6/19 15:09
*
*/
@Service
public class CourseServiceImpl implements CourseService{

    @Resource
    private CourseMapper courseMapper;

    //@CacheEvict 删除的注解通过对象id在redis中删除数据
    @CacheEvict(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#courseId")
    @Override
    public int deleteByPrimaryKey(Integer courseId) {
        return courseMapper.deleteByPrimaryKey(courseId);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#result.courseId")
    @Override
    public Course insert(Course record) {
        courseMapper.insert(record);
        return record;
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#result.courseId")
    @Override
    public Course insertSelective(Course record) {
        courseMapper.insertSelective(record);
        return record;
    }


    //@Cacheable 返回对象 通过对象id在redis中创建对象保存,方便下次使用
    @Cacheable(cacheNames ="com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#courseId")
    @Override
    public Course selectByPrimaryKey(Integer courseId) {
        return courseMapper.selectByPrimaryKey(courseId);
    }

    //@CachePut 要返回对象 修改通过返回的对象id来在redis修改对象数据
    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#result.courseId")
    @Override
    public Course updateByPrimaryKeySelective(Course record) {
        courseMapper.updateByPrimaryKeySelective(record);
        return record;
    }

    //@CachePut 要返回对象 修改通过返回的对象id来在redis修改对象数据
    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#result.courseId")
    @Override
    public Course updateByPrimaryKey(Course record) {
        courseMapper.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public DataGridView CourseList(CourseVo course) {
        Page<Object> page = PageHelper.startPage(course.getPage(),course.getLimit());//分页数据格式
        List<Course> noticeVos = courseMapper.CourseList(course);//返回的全部数据
        return new DataGridView(page.getTotal(), noticeVos);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#result.courseId")
    @Override
    public CourseVo CourseAdd(CourseVo courseVo) {
        courseMapper.insert(courseVo);
        return courseVo;
    }
    //@CachePut 要返回对象 修改通过返回的对象id来在redis修改对象数据
    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#result.courseId")
    @Override
    public CourseVo CourseUpdate(CourseVo courseVo) {
        courseMapper.updateByPrimaryKey(courseVo);
        return courseVo;
    }

    //@CacheEvict 删除的注解通过对象id在redis中删除数据
    @CacheEvict(cacheNames = "com.smart_campus.teg.service.impl.CourseServiceImpl",key = "#courseId")
    @Override
    public void CourseDelete(Integer courseId) {
        courseMapper.deleteByPrimaryKey(courseId);
    }


}
